package com.mapgis.utils

import java.io._
import java.util.Properties

import com.mapgis.constant.Constants

import scala.io.Source


object IOutil {

  //  def writeFile(fileName: String, str: String): Unit ={
  //
  //    val writer = new FileWriter(s"/opt/module/project/sjwq/data_sync/wait_sync/${fileName}.txt",true)
  //    //    val writer = new PrintWriter(new File("E:\\RichieProject\\dataGet\\src\\main\\resources\\time.txt"))
  //    writer.write(str)
  //    writer.close()
  //  }

  def writeFile(fileName: String, str: String): Unit = {
    val writer = new PrintWriter(new File(s"/opt/module/project/sjwq/data_sync/table_field/${fileName}.txt"))
    //    val writer = new PrintWriter(new File("E:\\RichieProject\\dataGet\\src\\main\\resources\\time.txt"))
    writer.println(str)
    println(s"输出到文件 ${fileName}.txt 成功")
    writer.close()
  }

  def writeIDListToFile(fileName: String, targetList: List[String], cutValue: Int): Unit = {
    val writer = new PrintWriter(new File(s"/opt/module/project/sjwq/data_sync/wait_sync/${fileName}.txt"))
    //    val writer = new PrintWriter(new File("E:\\RichieProject\\dataGet\\src\\main\\resources\\time.txt"))

    for (i <- 0 to targetList.size / cutValue) {
      var newIdList = targetList.slice(i * Constants.ORACLE_CURSOR, (i + 1) * Constants.ORACLE_CURSOR)
      val str = s" id in ('${newIdList.mkString("','")}')"
      //      println(str)

      writer.println(str)
    }

    println(s"输出到文件 ${fileName}.txt 成功")
    writer.close()
  }

  def clearFile(fileName: String): Unit = {

    val writer = new PrintWriter(new File(s"/opt/module/project/sjwq/data_sync/wait_sync/${fileName}.txt"))
    //    val writer = new PrintWriter(new File("E:\\RichieProject\\dataGet\\src\\main\\resources\\time.txt"))
    writer.println("")
    writer.close()
  }

  def getOneProperty(fileName: String, propertyName: String) = {
    val prop = new Properties();
    val fileInputStream = new FileInputStream("/opt/module/migration/" + fileName)
    prop.load(new InputStreamReader(fileInputStream, "UTF-8"))
    prop.getProperty(propertyName)
  }

//  def readFile(filePath: String): Unit = {
//    var text = ""
//    try {
//      // 防止文件建立或读取失败，用catch捕捉错误并打印，也可以throw
//
//      var stringBuilder = new StringBuilder();
//      // 读入TXT文件
//      // 绝对路径或相对路径都可以，这里是绝对路径，写入文件时演示相对路径
//      // 要读取以上路径的input。txt文件
//      val filename = new File(filePath);
//      // 建立一个输入流对象reader,..
//      val reader = new InputStreamReader(new FileInputStream(filename));
//      // 建立一个对象
//      val br = new BufferedReader(reader);
//      var line = br.readLine();
//      while (line != null) {
//        // 一次读入一行数据
//        line = br.readLine();
//        stringBuilder.append(line);
//      }
//      br.close();
//      reader.close();
//      System.out.println(stringBuilder.toString());
//      text = stringBuilder.toString()
//    }catch {
//      case e: IOException => e.printStackTrace()
//      case e: Exception => e.printStackTrace()
//    }
//    text
//  }

  def readFile(filePath: String) = {
    var text = ""
    try {
      // 防止文件建立或读取失败，用catch捕捉错误并打印，也可以throw

      val filename = new File(filePath);
      val source = Source.fromFile(filename,"UTF-8")
      text = source.getLines().toList.mkString("\n")
      source.close()

    }catch {
      case e: IOException => e.printStackTrace()
      case e: Exception => e.printStackTrace()
    }
    println(text)
    text
  }
}
